<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>新增系统用户</title>
<link type="text/css" rel="stylesheet"
	href="${ctx }/js/ztree/css/zTreeStyle/zTreeStyle.css" />
<script type="text/javascript" src="${ctx }/js/bootstrap/jquery.js"></script>
<script type="text/javascript">
	$(function() {
		$("#roleForm").validationEngine('attach', {promptPosition : "bottomLeft", scroll:"false"});
	});

	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};

	var zNodes =${permissionTree};
	
	var code;
	
	function setCheck() {
		var zTree = $.fn.zTree.getZTreeObj("permissionTree"),
		type =  { "Y" : "ps", "N" : "ps"};
		zTree.setting.check.chkboxType = type;
	}
	function showCode(str) {
		if (!code) code = $("#code");
		code.empty();
		code.append("<li>"+str+"</li>");
	}
	
	$(document).ready(function(){
		$.fn.zTree.init($("#permissionTree"), setting, zNodes);
		setCheck();
		$("#py").bind("change", setCheck);
		$("#sy").bind("change", setCheck);
		$("#pn").bind("change", setCheck);
		$("#sn").bind("change", setCheck);
	});

	/**
	 * 获取选中的菜单id
	 */
	function getCheckedMenuIds(){
		var selectedMenuIds = "";
		var zTreeMenu = $.fn.zTree.getZTreeObj("permissionTree");
		var nodes = zTreeMenu.getCheckedNodes(true);
		if (nodes != "") {
			for (var i=0;i<nodes.length;i++) {
				selectedMenuIds += nodes[i].id+",";
			}
			selectedMenuIds = selectedMenuIds.substr(0,selectedMenuIds.length-1);
		}
		
		return selectedMenuIds;
	}
	
	function submitForm(){
		if($("#roleForm").validationEngine('validate')){
			$.ajax({
				url:"${ctx}/role/repetitionValidate",
				type:"POST",
				dataType:"text",
				data:"roleName="+$("#roleName").val()+"&status=1&updateId="+$("#id").val(),
				success:function(data){
					if(data == "1"){
						$('#roleName').validationEngine('showPrompt', '该角色名已存在，请重新输入！', 'error');
					}else if(data == "0"){
						var checkedIds = getCheckedMenuIds();
						$("#checkedIds").val(checkedIds);
						
						$("#roleForm").ajaxSubmit({
					        type: "post",
					        dataType: "text",
					        success: function(data){
					        	if(data == 'true'){
					        		art.dialog({content:"保存成功！",ok:function(){window.location.href="${ctx}/role/list";}});
					        	}else{
					        		art.dialog({content:"保存失败！",ok:true});
					        	}
					        }
					    }); 
					}
				}
			});
		}
	}
</script>
</head>
<body>
	<div class="breadcrumbs" id="breadcrumbs">
		<script type="text/javascript">
        try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
    </script>

		<ul class="breadcrumb">
			<li><i class="ace-icon fa fa-home home-icon"></i> <a
				href="${ctx }/home/home">首页</a></li>
			<li class="active">系统管理</li>
			<li class="active">角色管理</li>
		</ul>
		<!-- .breadcrumb -->
	</div>
	<div class="page-content">
		<div class="page-header">
			<h1>
				角色管理 <small> <i class="ace-icon fa fa-double-angle-right"></i> 修改角色
				</small>
			</h1>
		</div>
		<!-- /.page-header -->
		<form class="form-horizontal" id="roleForm"
			action="${ctx }/role/update" method="post">
			<input type="hidden" id="id" name="id" value="${entity.id }" /> <input
				type="hidden" id="checkedIds" name="checkedIds" value="" />
			<div class="row">
				<div class="col-xs-12">
					<div>
						<div class="form-group">
							<label for="inputEmail3" class="col-sm-2 control-label"><b
								class="red">*</b><strong>角色名称</strong></label>
							<div class="col-sm-6">
								<input type="text" class="form-control validate[required]"
									id="roleName" name="roleName" maxlength="20"
									value="<c:out value="${entity.roleName }" escapeXml="true"></c:out>">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label"><b class="red">*</b><strong>状态</strong></label>
							<div class="col-sm-6">
								<select:option type="status" id="status" name="status"
									showValue="false" styleClass="form-control"
									value="${entity.status }" />
							</div>
						</div>
						<div class="form-group">
							<label for="inputPassword3" class="col-sm-2 control-label"><b
								class="red">*</b><strong>备注</strong></label>
							<div class="col-sm-9">
								<textarea class="form-control validate[required]" rows="3"
									id="rmk" name="rmk" maxlength="100">${entity.rmk }</textarea>
							</div>
						</div>
					</div>
					<div class="col-xs-12"
						style="text-align: center; margin-top: 20px;">
						<button type="button" class="btn btn-info" onclick="submitForm()">提交</button>
						<button type="button" class="btn btn-default"
							onclick="javascript:window.location.href='${ctx}/role/list'">返回</button>
					</div>
					<h3 class="header smaller lighter gray" style="margin-top: 50px;">权限选择</h3>
					<div class="col-xs-12">
						<ul id="permissionTree" class="ztree"></ul>
					</div>
				</div>
			</div>
		</form>
	</div>
</body>
</html>